import { GlobalValue } from "../utils";

const roleBuilder = {
    /** @param {Creep} creep **/
    run: function (creep) {
        if (creep.memory.building && creep.store[RESOURCE_ENERGY] === 0) {
            creep.memory.building = false;
            creep.say("🔄 harvest");
        }

        if (!creep.memory.building && creep.store.getFreeCapacity() === 0) {
            creep.memory.building = true;
            creep.say("🚧 build");
        }

        if (creep.memory.building) {
            // const targets = creep.room.find(FIND_CONSTRUCTION_SITES);
            const targets = GlobalValue.construction_sites;
            if (targets.length) {
                if (creep.build(targets[0]) === ERR_NOT_IN_RANGE) {
                    creep.moveTo(targets[0], {
                        visualizePathStyle: { stroke: "#ffffff" }
                    });
                }
            } else {
                // console.log("Builder,找不到要修的建筑，那我去repair")

                // 初筛，指定类型和血量阈值
                // let needRepairtargets = creep.room.find(FIND_STRUCTURES, {
                //     filter: (structure) => {
                //         return (
                //             (structure.structureType === STRUCTURE_EXTENSION ||
                //                 structure.structureType === STRUCTURE_SPAWN ||
                //                 structure.structureType ===
                //                     STRUCTURE_CONTAINER ||
                //                 structure.structureType === STRUCTURE_TOWER ||
                //                 structure.structureType === STRUCTURE_ROAD ||
                //                 structure.structureType === STRUCTURE_WALL ||
                //                 structure.structureType ===
                //                     STRUCTURE_RAMPART) &&
                //             structure.hits < structure.hitsMax * 0.5
                //         );
                //     }
                // });

                const targets = GlobalValue.structures;
                let needRepairtargets = targets.filter((structure) => {
                    // 低于50%再修，不然被堵在某一个建筑不走了
                    return structure.hits < structure.hitsMax * 0.5;
                });
                // 再筛
                needRepairtargets = _.filter(needRepairtargets, (structure) => {
                    if (
                        (structure.structureType === STRUCTURE_WALL ||
                            structure.structureType === STRUCTURE_RAMPART) &&
                        structure.hits > 10000
                    ) {
                        // 如果是WALL，修到大于10000就不修了
                        return false;
                    } else if (structure.hits > structure.hitsMax * 0.75) {
                        // 修到75%就不用修了
                        return false;
                    } else {
                        // 可以修
                        return true;
                    }
                });

                if (needRepairtargets.length > 0) {
                    const repairTarget =
                        needRepairtargets[needRepairtargets.length - 1];
                    // 准备修
                    if (creep.repair(repairTarget) === ERR_NOT_IN_RANGE) {
                        creep.moveTo(repairTarget, {
                            visualizePathStyle: { stroke: "#ff0000" }
                        });
                    }
                }
            }
        } else {
            // const sources = creep.room.find(FIND_SOURCES);
            const sources = GlobalValue.sources;
            if (creep.harvest(sources[0]) === ERR_NOT_IN_RANGE) {
                creep.moveTo(sources[0], {
                    visualizePathStyle: { stroke: "#ffaa00" }
                });
            }
        }
    }
};

export { roleBuilder };
